package com.hanko.socket;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @Author: hanko
 * @Date: 2021-6-16 17:29
 */
@Slf4j
public class Client {

    @SneakyThrows
    public static void main(String[] args) {

        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 50000);
            socket.setSoTimeout(2000);
            DataInputStream in = new DataInputStream(socket.getInputStream());
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            String data = "12345678";
            out.writeUTF(data);
            log.info("发送数据：{}", data);
            String res = in.readUTF();
            log.info("返回的数据：{}", res);
            out.close();
            in.close();
        } catch (IOException e) {
            System.out.println("进入异常处理socket状态：" + socket.isClosed());
            e.printStackTrace();
        }finally {
            if (socket != null) {
                socket.close();
                System.out.println("进入finally socket状态：" + socket.isClosed());
            }
        }

    }
}
